package com.shujia.wyh.day14.tcpdemo2;

import java.io.*;
import java.net.Socket;

/*
    模拟网络中客户端上传文件给服务器
    E:\shujia\owner\linux\IP等关系.txt
 */
public class ClientDemo {
    public static void main(String[] args) throws Exception {
        //创建客户端的Socket对象
        Socket socket = new Socket("192.168.6.80", 10086);

        //创建字符输入缓冲流对象
        BufferedReader br = new BufferedReader(new FileReader("E:\\shujia\\owner\\linux\\IP等关系.txt"));

        //获取通道中的输出流对象
        OutputStream outputStream = socket.getOutputStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        BufferedWriter bw = new BufferedWriter(outputStreamWriter);

        //读取文件中的内容
        String line = null;
        while ((line = br.readLine()) != null) {
            bw.write(line);
            bw.write("\r\n");
            bw.flush();
        }

        //当数据写完后，告诉服务器端没有数据过来了
        socket.shutdownOutput();

        //获取通道中的输入流对象
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int length = inputStream.read(bytes);
        String info = new String(bytes, 0, length);
        System.out.println("服务端的反馈：" + info);

        //关闭连接
        socket.close();

    }
}
